package Tier1;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import Shared._Config;

/**
 * Hérite de application Client et implémente le protocole RMI
 * 
 */
public class UserRMI {

    public class CallbackImpl implements Tier2.RMI_PartyManager.Callback {
	private User.Callback _proxy = null;
	public void setProxy(User.Callback proxy) { _proxy = proxy; }
	
	@Override
	public void callback(String msg) {
	    if(_proxy!=null) { _proxy.say(msg); } 
	    else { System.out.println(msg); }
	}
    }
    
    public static void main(String args[]) throws Exception {

	User user = new User();
	
	// Recupère le service <PartyManager>
	
	Registry registry = LocateRegistry.getRegistry(_Config.RMI_host, _Config.RMI_port);
	Tier2.RMI_PartyManager.PartyManager service = (Tier2.RMI_PartyManager.PartyManager) registry.lookup(_Config.party_manager);
	
	// Ajoute le callback
	User.Callback proxy = user.new Callback();
	UserRMI.CallbackImpl callbackImpl = new UserRMI().new CallbackImpl();
	callbackImpl.setProxy(proxy);
	
	Tier2.RMI_PartyManager.Callback callback = (Tier2.RMI_PartyManager.Callback) UnicastRemoteObject.exportObject(callbackImpl, 0);
	service.callback(user.hashCode() + "", callback);
	
	// Rend la main à <User>
	user.init(service, null, proxy).run();
    }
}
